<?php

namespace App\Services\Upload;

use App\Models\Attachment;

/**
 * 站点服务
 * @package App\Services
 */
class UploadService
{

    /**
     * 站点上传
     * @param UploadedFile | String $file
     * @return mixed
     * @throws BindingResolutionException
     */
    public function make($file)
    {
        $driver = config('site.upload.driver', 'local');
        return $this->$driver($file);
    }


    /**
     * 本地上传
     * @param [type] $file
     * @return void
     */
    public function local($file)
    {
        $path = $file->store(date('y/m'), 'attachment');
        $path = url("attachment/{$path}");
        return $this->save($file, $path);

    }


    /**
     * 用户保存数据
     *
     * @param [type] $file
     * @param [type] $path
     * @return void
     */
    protected function save($file, $path)
    {
        $realFile = $file->getRealPath();
        return Attachment::create([
            'path' => $path,
            'name' => $file->getClientOriginalName(),
            'size' => round(filesize($realFile) / 1024, 2),
            'extension' => $file->extension()
        ]);
    }


}
